//index.js
//获取应用实例
const common = require('../../../../public.js')
const amap = require('../../../../libs/amap-wx')
Page({
    data: {
        BannerList: [],
        banner: [],
        actData: [],
        pageNum: 0,
        pageSize: 10,
        cityName: '',
        noMoreData: false,
        isLoadingBusy: false,
        refreshing: false,
        city: null,
        selected: 3,
        search: ''
    },
    //事件处理函数
    onLoad: function () {
        this.reloadData()
        this.getBannerInfo()

    },
    //时间转换
    time(time) {
        return common.dateFormat('MM/dd HH:mm', time)
    },
    reloadData() {
        getApp().getLocation(data => {
            const currentCity = (data && data.regeocodeData && data.regeocodeData.addressComponent) ?
                data.regeocodeData.addressComponent.city : ''
            this.setData({
                city: currentCity
            })
            wx.startPullDownRefresh()  //开始下拉刷新
        }, () => {
            // 定位失败
            wx.startPullDownRefresh()
        })
    },
    chooseLocation() { // 点击选择地理位置
        const that = this
        wx.chooseLocation({
            success: (data) => {
                let lat = data.latitude //纬
                let lon = data.longitude // 经
                // https://restapi.amap.com/v3/geocode/regeo?output=xml&location=116.310003,39.991957&key=<用户的key>
                // https://developer.amap.com/api/wx/guide/get-data/regeo
                const app = getApp()
                let AMAP = new amap.AMapWX({
                    key: app.globalData.amapKey
                })
                AMAP.getRegeo({
                    location: `${lon},${lat}`,
                    success: function (data) {
                        app.cacheLocationData(data[0])
                        that.reloadData()
                    },
                    fail: function (data) { }
                })
            }
        })
    },
    onSearch() {
        this.onClickSearch()
    },
    onClickSearch() {
        if (!this.data.search == "") {
            this.refresh()
        } else {
            common.showToast('您还没有输入任何文字~')
        }
    },
    //获取首页banner图数据
    getBannerInfo() {
        common.getJson('/content/activitylist', {}, (res) => {
            this.setData({
                BannerList: res.data
            })
        })
    },

    refresh() {
        this.getData(true)
    },
    loadMore() {
        this.getData(false)
    },
    getData(refresh = false) {
        // 当前非刷新状态，正在加载数据或已经没有数据，不需要再次请求数据
        if (!refresh && (this.data.isLoadingBusy || this.data.noMoreData)) {
            return
        }

        if (!refresh) {
            this.setData({
                isLoadingBusy: true
            })
        } else {
            this.setData({
                refreshing: true,
                noMoreData: false
            })
        }

        const that = this
        const pageNum = refresh ? 1 : this.data.pageNum + 1
        const tempJson = {
            pageNum,
            pageSize: that.data.pageSize,
            cityName: that.data.cityName,
            searchContent: that.data.search
        }
        common.post('/storeActivity/selectActivityList', tempJson, function (res) {
            if (res.status === 200) {
                let data = res.data
                if (data == '' || data == null || data == undefined) {
                    that.setData({
                        noMoreData: true
                    })
                } else {
                    let actData = that.data.actData;
                    for (let i = 0; i < data.length; i++) {

                        data[i].enrollEndTime = that.time(data[i].enrollEndTime)
                        data[i].enrollStartTime = that.time(data[i].enrollStartTime)
                        data[i].holdingStarttime = that.time(data[i].holdingStarttime)
                        data[i].holdingEndtime = that.time(data[i].holdingEndtime)
                    }
                    // 设置数据
                    that.setData({
                        pageNum, // 请求成功后，更新页码
                        actData: refresh ? data : actData.concat(data)
                    })
                }
            } else {
                common.showRequestErrMsg(res)
            }
        },
            function () {
                // 加载完成回调
                if (refresh) {
                    //停止下拉刷新
                    wx.stopPullDownRefresh()
                    that.setData({
                        refreshing: false
                    })
                } else {
                    that.setData({
                        isLoadingBusy: false
                    })
                }
            })
    },
    convertTime(time) {
        const tba = time.map((item, index) => {
            const startTime = this.time(item.holdingStarttime);
            const ensTime = this.time(item.holdingEndtime);
            const tabs = {
                id: item.id,
                title: item.title,
                holdingStarttime: startTime,
                holdingEndtime: ensTime,
                holdingAddress: item.holdingAddress,
                posterImage: item.posterImage,
                cid: item.cid,
                userId: item.userId,
                status: item.status,
                shareNum: item.shareNum,
                browseNum: item.browseNum,
                ticketPrice: item.ticketPrice
            }
            return tabs;
        })
        this.setData({
            actData: tba
        })
    },
    // // 封装时间控件方法
    time(time) {
        return common.dateFormat('yyyy-MM-dd HH:mm', time)
    },
    onPullDownRefresh: function () {
        this.refresh()
    },
    onReachBottom() {
        this.loadMore()
    }
})